import { Injectable } from '@nestjs/common';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { UploadFileDto } from './dto/upload-file.dto';

@Injectable()
export class UploadService {
  static getStorage() {
    return diskStorage({
      destination: './uploads',  // 确保这个目录存在
      filename: (req, file, cb) => {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        const ext = extname(file.originalname);
        const filename = `${uniqueSuffix}${ext}`;
        cb(null, filename);
      },
    });
  }


  getUploadedFileUrl(file: Express.Multer.File): UploadFileDto {
    const response = {
      url: `http://localhost:3000/uploads/${file.filename}`,
    };
    return response;
  }
}
